package com.flynn.rock.config;


import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

//@OpenAPIDefinition(
//        info = @Info(
//                title = "Swagger3",
//                version = "1.0",
//                description = "Swagger3使用演示",
//                contact = @Contact(name = "TOM")
//        ),
//        security = @SecurityRequirement(name = "JWT"),
//        externalDocs = @ExternalDocumentation(description = "参考文档",
//                url = "https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations"
//        )
//)
//@SecurityScheme(type = SecuritySchemeType.HTTP, name = "JWT", scheme = "bearer", in = SecuritySchemeIn.HEADER)
@Configuration
public class SysSwagger3Config {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .components(setToken())
                .info(
                        new Info().title("BeWorth.Api.Doc")
                                .extensions(Map.of(
                                        "x-audience", "external-partner",
                                        "x-application-id", "APP-12345"
                                ))
                                .description("BeWorth Api Doc")
                                .version("1.0")
                )
                .security(securityList());
    }

    /**
     * 在 swagger 页面上点击 Authorize 按钮，记录输入的 token
     */
    private Components setToken() {
        return new Components().addSecuritySchemes("Authorization",
                new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT"));
    }

    /**
     * 给每个请求的 header 上加一个 Authorization，值就是 setToken() 方法记录的 token，比如：Bearer eyJhbGciOiJSUxx
     */
    private List<SecurityRequirement> securityList() {
        SecurityRequirement securityRequirement = new SecurityRequirement();
        securityRequirement.addList("Authorization");
        return List.of(securityRequirement);
    }
}
